/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * GtkModelica
 * Copyright (C) Jorge Eduardo Cardona Gaviria 2008 <jorgeecardona@gmail.com>
 * 
 * GtkModelica is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GtkModelica is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>
#include <glib/gstdio.h>
#include "gui.hpp"
#include "modelica.hpp"
#include "modelica-grammar.hpp"



Gui::Gui(Glib::RefPtr<Gnome::Glade::Xml> refXml)
{
    // TODO: Put some order here!!!!
	
	this->refXml = refXml;
	
	/*************** Used widgets ***************/ 
	
	main_window = NULL;
	refXml->get_widget("main_window",main_window);
	main_window->maximize();
	
	select_block_window = NULL;
	refXml->get_widget("select_block_window",select_block_window);
		
	treeview_files = NULL;
	refXml->get_widget("treeview_files", treeview_files);
	workspace_files_columns = new WorkspaceFilesColumns();
	workspace_files_store	= Gtk::TreeStore::create(*workspace_files_columns);
	treeview_files->set_model(workspace_files_store);
	treeview_files->append_column("Name",workspace_files_columns->name);
	treeview_files->append_column("Path",workspace_files_columns->path);
	treeview_files->append_column_editable("On library",workspace_files_columns->on_library);
	treeview_files->get_column(0)->set_resizable(true);
	treeview_files->get_column(1)->set_resizable(true);
	treeview_files->get_column(2)->set_resizable(true);
	
	treeview_library = NULL;
	refXml->get_widget("treeview_library", treeview_library);
	library_columns = new LibraryColumns();
	library_store	= Gtk::TreeStore::create(*library_columns);
	treeview_library->set_model(library_store);
	treeview_library->append_column("Name",library_columns->name);
	treeview_library->append_column("Type",library_columns->type);;
	treeview_library->get_column(0)->set_resizable(true);
	treeview_library->get_column(1)->set_resizable(true);

	
	
	iconview_blocks = NULL;
	refXml->get_widget("iconview_blocks", iconview_blocks);
	
	label_description = NULL;
	refXml->get_widget("label_description", label_description);
	
	// Set Open Dialog
	open_dialog = new Gtk::FileChooserDialog::FileChooserDialog("Name", Gtk::FILE_CHOOSER_ACTION_OPEN);
	open_dialog_filter = new Gtk::FileFilter();	
	open_dialog->signal_response().connect(sigc::mem_fun(*this, &Gui::dialog_open_response));
	
	// Add accept and cancel button
	open_dialog->add_button("Select", Gtk::RESPONSE_OK);
	open_dialog->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
	
	// Set filter
	open_dialog_filter->add_pattern("*.mo");	
	
	
	/************* Connect signals **************/
	
	/*Gtk::ToolButton *add_block_toolbutton = NULL;
	refXml->get_widget("add_block_toolbutton", add_block_toolbutton);
	add_block_toolbutton->signal_clicked().connect(sigc::mem_fun(*this, &Gui::on_add_block_toolbutton_clicked));*/

	Gtk::Button *select_block_cancel_button = NULL;
	refXml->get_widget("select_block_cancel_button", select_block_cancel_button);
	select_block_cancel_button->signal_clicked().connect(sigc::mem_fun(*this, &Gui::on_select_block_cancel_button_clicked));
	
	//treeview_packages->signal_cursor_changed().connect(sigc::mem_fun(*this, &Gui::on_treeview_packages_cursor_changed));
	
	// Menu File->Open (Open a modelica file)
	Gtk::MenuItem		*menu_file_open = NULL;
	refXml->get_widget("menu_file_open", menu_file_open);
	menu_file_open->signal_activate().connect(sigc::mem_fun(*this, &Gui::on_menu_file_open_activate));
	
	
	//Dialogs Signals
	
	/********************************************/
	
	/******* Fill tree store with blocks ********/
	
	/*blocks = new Blocks();	

	packages_tree_store = Gtk::TreeModelFilter::create(blocks->tree_store);
	packages_tree_store->set_visible_func(sigc::mem_fun(*blocks, &Blocks::on_package));
	
	treeview_packages->set_model(packages_tree_store);
	treeview_packages->append_column("Categoria",blocks->model_record->name);	*/
	
	/********************************************/
	
	Gnome::Canvas::Canvas* main_canvas = 0;
	refXml->get_widget("main_canvas",main_canvas);
		
	Gnome::Canvas::Group* grupo = main_canvas->root(); 
	Gnome::Canvas::Ellipse* elipse = new Gnome::Canvas::Ellipse(*grupo, 500,600,700,700);
	elipse->set_property("outline_color", Glib::ustring("red"));
	elipse->set_property("fill_color", Glib::ustring("red"));
	main_canvas->show();
}

void Gui::on_add_block_toolbutton_clicked()
{
	select_block_window->set_property("visible",1);
}

void Gui::on_select_block_cancel_button_clicked()
{
	select_block_window->set_property("visible",0);
}

void Gui::on_menu_file_open_activate()
{	
	// Open dialog
	open_dialog->set_filter(*open_dialog_filter);
	open_dialog->show();
	
}



void Gui::dialog_open_response(int response_id)
{
	//Hide Dialog
	open_dialog->hide();
	
	switch(response_id)
	{
		case Gtk::RESPONSE_OK:
		{	
			// Get content of file.
			Glib::ustring filename = open_dialog->get_filename();
			Glib::ustring folder = open_dialog->get_current_folder();
			
  			std::cout << "File selected: " <<  filename << std::endl;

		
			string content =	Glib::file_get_contents(filename);
									
			
			if (read_string(content))
			{
				std::cout << "-------------------------\n";
				std::cout << "Parsing succeeded\n";
				std::cout << "-------------------------\n";
				
				// Add this file to the file tab.
				Gtk::TreeModel::iterator iter = workspace_files_store->append();
				Gtk::TreeModel::Row row = *iter;
				row[workspace_files_columns->name] = filename.substr(folder.size() + 1, filename.size() - folder.size() - 1);
				row[workspace_files_columns->path] = folder;
				row[workspace_files_columns->on_library] = true;
							
			}
			else
			{
				std::cout << "-------------------------\n";
				std::cout << "Parsing failed\n";
//				std::cout << "stopped at: \": " << info.stop << "\"\n";
				std::cout << "-------------------------\n";
			}
			
			// TODO: Delete this! should be on a add to workspace button.
																																				
																																				
																																				

			// Draw elements on screen
			
			
			break;
		}
		case Gtk::RESPONSE_CANCEL:
		{
			// Do nothing.
			break;
		}
		default:
		{
		}
			
	}
}





void Gui::on_treeview_packages_cursor_changed()
{
/*	Glib::RefPtr<Gtk::TreeSelection> tree_selection = treeview_packages->get_selection();
	Gtk::TreeModel::iterator iter = tree_selection->get_selected();
	
	if (iter)
	{
		Gtk::TreeModel::Row row = *iter;
//		label_description->set_label(row[blocks->model_record->description]);	
		
		Gtk::TreePath tree_path = Gtk::TreePath(iter);
		
	//	blocks_list_store = TreeModelFilterList::create(blocks->tree_store,tree_path);
	 	std::cout << "1" << std::endl;
		//blocks_list_store->set_visible_func(sigc::mem_fun(*blocks, &Blocks::on_block));
	 	std::cout << "2" << std::endl;
		
		iconview_blocks->set_model(blocks_list_store);
	 	std::cout << "3" << std::endl;
	//	iconview_blocks->set_text_column(blocks->model_record->name);
	 	std::cout << "4" << std::endl;
		
		
		//Glib::RefPtr<TreeModelFilterList> p = TreeModelFilterList::create(blocks->tree_store,tree_path);
		//p->get_flags();
		
	}
*/
}

Gtk::TreeModelFlags TreeModelFilterList::get_flags() const
{
	std::cout << "Entro" << std::endl;
	return Gtk::TREE_MODEL_LIST_ONLY;
}

Glib::RefPtr<TreeModelFilterList> TreeModelFilterList::create(const Glib::RefPtr<Gtk::TreeModel>& child_model, const Gtk::TreeModel::Path& virtual_root)
{
	Glib::RefPtr<Gtk::TreeModelFilter> p = Gtk::TreeModelFilter::create(child_model, virtual_root);
	return Glib::RefPtr<TreeModelFilterList>::cast_dynamic(p);
}
